package com.cgbl.cg.vm.work

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.util.ToastUtils
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class NextReleaseWorkViewModel(application: Application) : GLBaseViewModel(application) {
    var title = ""
    var direction = ""
    var imgList = ArrayList<String>()
    val imgUrl = ObservableField<String>()

    val typeText = ObservableField<String>()
    val typeFirstText = ObservableField<String>()
    var type = ""
    val declareText = ObservableField<String>()
    var declare = ""
    val copyrightTypeText = ObservableField<String>()
    var copyrightType = ""
    val save = ObservableField<Boolean>(false)
    var tagList = arrayListOf<String>()
    val tagText = ObservableField<String>()

    val typeList = arrayListOf<CourseAllTypeBean>()
    val declareList = arrayListOf("原创", "转载", "临摹", "自译")
    val copyrightTypeList = arrayListOf(
        "署名-非商业性使用-禁止演绎", "署名-非商业性使用-相同方式共享",
        "署名-非商业性使用", "署名-禁止演绎", "署名-相同方式共享", "署名", "不声明"
    )

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val showTypeEvent = SingleLiveEvent<Void>()
        val showDeclareEvent = SingleLiveEvent<Void>()
        val showCopyrightTypeEvent = SingleLiveEvent<Void>()
        val showTagEvent = SingleLiveEvent<Void>()
    }

    val showTypeCommand = BindingCommand<Any>(BindingAction {
        uc.showTypeEvent.call()
    })

    val showDeclareCommand = BindingCommand<Any>(BindingAction {
        uc.showDeclareEvent.call()
    })

    val showCopyrightTypeCommand = BindingCommand<Any>(BindingAction {
        uc.showCopyrightTypeEvent.call()
    })

    val showTagCommand = BindingCommand<Any>(BindingAction {
        uc.showTagEvent.call()
    })

    val confirmCommand = BindingCommand<Any>(BindingAction {
        createWork()
    })

    fun allType() {
        typeList.clear()
        WorkServiceFactory.allType()
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
                override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                    typeList.addAll(t.data)
                }
            })
    }

    fun createWork() {
        val map = hashMapOf<String, Any>(
            "contentImages" to imgList,
            "coverImages" to (imgUrl.get() ?: ""),
            "isSave" to if (save.get() == true) {
                1
            } else {
                0
            },
            "tags" to tagList,
            "type" to 1,
            "workExplain" to direction,
            "workName" to title
        )
        if (!TextUtils.isEmpty(declare))
            map["creationDescType"] = declare.toInt()

        if (!TextUtils.isEmpty(copyrightType))
            map["copyrightType"] = copyrightType.toInt()

        if (!TextUtils.isEmpty(type))
            map["workTypeId"] = type.toInt()

        showLoadingDialog()
        WorkServiceFactory.createWorks(map)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    ToastUtils.showShort("新建成功")
                }
            })
    }
}